package com.infra.micro.quartz;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.infra.common.util.AuthUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableKnife4j
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build().globalRequestParameters(authorizationParameter());
    }

    private List<RequestParameter> authorizationParameter() {
        List<RequestParameter> parameters = new ArrayList<>();

        RequestParameterBuilder tokenBuilder = new RequestParameterBuilder();
        tokenBuilder
                .name(AuthUtils.AUTHORIZATION)
                .description("授权码")
                .required(false)
                .in("header")
                .accepts(Collections.singleton(MediaType.APPLICATION_JSON))
                .build();

        parameters.add(tokenBuilder.build());
        return parameters;
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("调度服务在线文档") //页面标题
                .contact(new Contact("PD", "http://www.ityee.cn/", "hn-man@live.cn"))//创建人
                .version("1.0")//版本号
                .description("提供定义任务的管理，以及运行日志信息管理功能，支持集群模式")//描述
                .build();
    }
}
